package com.mdd.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.common.entity.BusinessWf;
import com.mdd.common.enums.AlbumEnum;
import com.mdd.common.enums.WxSencePageEnum;
import com.mdd.common.handler.BASE64DecodedMultipartFile;
import com.mdd.common.mapper.BusinessWfMapper;
import com.mdd.common.plugin.storage.StorageDriver;
import com.mdd.common.plugin.storage.UploadFilesVo;
import com.mdd.common.service.BusinessWiFiAppService;
import com.mdd.common.service.WxCommonService;
import com.mdd.common.util.StringUtils;
import com.mdd.common.util.UrlUtils;
import com.mdd.common.validate.business.BusinessWiFiApplyValidate;
import com.mdd.common.vo.business.BusinessWifiConnectDetailVo;
import com.mdd.common.vo.business.BusinessWifiDetailVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Objects;

@Service
@Slf4j
public class BusinessWiFiAppServiceImpl extends ServiceImpl<BusinessWfMapper, BusinessWf> implements BusinessWiFiAppService {


    @Resource
    WxCommonService wxCommonService;

    /**
     * 商户无线wifi申请
     *
     * @param applyValidate
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addBusinessWiFi(BusinessWiFiApplyValidate applyValidate) {
        LambdaQueryWrapper<BusinessWf> wrapper = new LambdaQueryWrapper<BusinessWf>().eq(BusinessWf::getUsername, applyValidate.getUsername()).eq(BusinessWf::getBssId, applyValidate.getBssId()).last("limit 1");

        BusinessWf business = getOne(wrapper);
        if (Objects.isNull(business)) {
            business = new BusinessWf();
        }
        BeanUtils.copyProperties(applyValidate, business);

        // 用户
        business.setCreateBy(applyValidate.getUserId());
        this.saveOrUpdate(business, wrapper);
        boolean flag = Objects.isNull(business) || (Objects.nonNull(business) && StringUtils.isBlank(business.getMiniUrl()));
        if (flag) {
            // 生成小程序码
            String base64Code = wxCommonService.generateMiniCode(WxSencePageEnum.BUSINESS_WIFI_DETAIL.getKey() + business.getId(), WxSencePageEnum.BUSINESS_WIFI_DETAIL.getDesc());
            //log.info("base64Code:{}" + base64Code);
            if (StringUtils.isNotBlank(base64Code)) {
                StorageDriver storageDriver = new StorageDriver();
                String base64 = "data:image/jpeg;base64," + base64Code;
                MultipartFile multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(base64);
                UploadFilesVo vo = storageDriver.upload(multipartFile, "image", AlbumEnum.IMAGE.getCode());
                // 更新
                business.setMiniUrl(vo.getUrl());
                this.updateById(business);
            }
        }
        return business.getId();
    }

    @Override
    public BusinessWifiDetailVo detail(Integer id) {
        BusinessWf detail = getById(id);
        Assert.notNull(detail, "数据不存在!");
        BusinessWifiDetailVo vo = new BusinessWifiDetailVo();
        BeanUtils.copyProperties(detail, vo);
        vo.setMiniUrl(UrlUtils.toAbsoluteUrl(detail.getMiniUrl()));
        return vo;
    }

    @Override
    public BusinessWifiConnectDetailVo detailConnect(Integer id) {
        BusinessWf detail = getById(id);
        BusinessWifiConnectDetailVo vo = new BusinessWifiConnectDetailVo();
        BeanUtils.copyProperties(detail, vo);
        vo.setMiniUrl(UrlUtils.toAbsoluteUrl(detail.getMiniUrl()));
        return vo;
    }

}
